package org.tema.web;

import java.util.ArrayList;
import java.util.Collection;

import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.SelectItem;

import org.tema.ejb.ReportBeanLocal;
import org.tema.ejb.TestPlanBeanLocal;
import org.tema.entities.Report;
import org.tema.entities.TestPlan;

public class ManageReportsBean {
	
	// navigation constants
	private static final String REPORTS_LIST = "reportsList";
	private static final String VIEW_REPORT= "viewReport";
	
	@EJB private ReportBeanLocal reportBean;
	@EJB private TestPlanBeanLocal planBean;
	
	
	private HtmlDataTable dataTable;
	private String searchWord;
	private Report report;
	private Collection <Report> reportsList = null;
	private ReportSearchTypes SearchDropDown;
	private String searchDropDownPlan;
	
	public  ManageReportsBean() {
		// constructor
	}
	
	public Report getReport() {
		return report;
	}

	public void setReport(Report report) {
		this.report = report;
	}


	public String getSearchWord() {
		return searchWord;
	}

	public void setSearchWord(String searchWord) {
		this.searchWord = searchWord;
	}

	public ReportSearchTypes getSearchDropDown() {
		return SearchDropDown;
	}

	public void setSearchDropDown(ReportSearchTypes searchDropDown) {
		SearchDropDown = searchDropDown;
	}

	public Collection<Report> getReportsList() {
		if(reportsList == null ){
			findAll();
		}
		return reportsList;
	}

	public void setReportsList(Collection<Report> reportsList) {
		this.reportsList = reportsList;
	}

	public String getSearchDropDownPlan() {
		return searchDropDownPlan;
	}

	public void setSearchDropDownPlan(String searchDropDownPlan) {
		this.searchDropDownPlan = searchDropDownPlan;
	}
	
	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

	public String generateReport() {
		TestPlan plan1 = planBean.findTestPlanBySummary(getSearchDropDownPlan());
		planBean.generateReport(plan1);
		// update reports list
		findAll();
		// add status message
		Status.addInfo("Report for plan " + plan1.getSummary() + " created. ");
		return REPORTS_LIST;
	}
	
	
	public String displayReport() {
		Report report1 = (Report) dataTable.getRowData();
		setReport( reportBean.findReportById(report1.getId()) );
		return VIEW_REPORT;
	}
	
	public String deleteReport(){
		report = (Report) dataTable.getRowData();
		reportBean.deleteReport(report);
		// update reports list
		findAll();
		// add status message
		Status.addInfo("Report " + " deleted. ");
		return REPORTS_LIST;
	}
	
	public String  selectSearch(){
		String result;
		switch (this.getSearchDropDown()) {
        case TESTPLAN_ID:  result = findByTestPlanId(this.getSearchWord()); break;
        case PRODUCT_ID:  result = findByProductId(this.getSearchWord()); break;
        case ALLREPORTS: result = findAll();
        default: result = findAll();
		}
		return result;
	}
	
	public String  findAll(){
		setReportsList( ( Collection <Report> ) reportBean.findAllReports());
		if ( getReportsList().isEmpty() ){
			// add status message
			Status.addInfo("No reports available.");
		}
		return REPORTS_LIST;
	}
	
	public String  findByTestPlanId(String searchWord){
		try{
			int id = Integer.parseInt(searchWord);
			setReportsList( reportBean.findReportsByTestPlan( id ) );
			if( this.reportsList.isEmpty() ){
				Status.addInfo("No reports for plan with id:" + id);
			}
		}
		 catch (EJBException e) {
				Status.addError(e.getCause().getMessage());
		}
		return REPORTS_LIST;
	}
	
	public String findByProductId(String searchWord){
		setReportsList( reportBean.findReportsByProductId(Integer.parseInt(searchWord) ) );
		if(  this.getReportsList().isEmpty() ){
			Status.addInfo("No reports available.");
			return REPORTS_LIST;
		}
		else{
			return REPORTS_LIST;
		}
	}
	
    public Collection<SelectItem> getItems() {
    	ReportSearchTypes[] values = ReportSearchTypes.values();
        Collection<SelectItem> list = new ArrayList<SelectItem>(values.length);
        for (ReportSearchTypes value : values) {            
        	list.add(new SelectItem(value, value.getLabel()));            
        } 
        return list;
    }
    
    public Collection<SelectItem> getPlans(){	
    	Collection <SelectItem> plansList = new ArrayList<SelectItem>( planBean.findAllTestPlans().size());  	
    	for(TestPlan planId : planBean.findAllTestPlans() ){
    		plansList.add( new SelectItem( planId.getSummary() ) );
    	}
    	return plansList;
    }
}
